package logic

import (
	"context"
	"fmt"
	"go-pod/pod-rpc/internal/svc"
	"go-pod/pod-rpc/pod"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListPodcastsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListPodcastsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListPodcastsLogic {
	return &ListPodcastsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// ListPodcasts 获取播客列表
func (l *ListPodcastsLogic) ListPodcasts(in *pod.ListPodcastsReq) (*pod.ListPodcastsResp, error) {
	// 调用分页查询方法
	podcasts, total, err := l.svcCtx.PodcastModel.FindPageList(l.ctx, in.Page, in.PageSize, in.Keyword)
	if err != nil {
		return nil, fmt.Errorf("查询播客列表失败: %w", err)
	}

	// 构建响应
	resp := &pod.ListPodcastsResp{
		Total: total,
		List:  make([]*pod.GetPodcastResp, 0, len(podcasts)),
	}

	// 转换数据
	for _, podcast := range podcasts {

		resp.List = append(resp.List, &pod.GetPodcastResp{
			Id:        int64(podcast.Id),
			Title:     podcast.Title.String,
			Content:   podcast.Content.String,
			Author:    podcast.Author.String,
			Tags:      podcast.Tags.String,
			CreatedAt: podcast.CreatedAt.Time.Format("2006-01-02 15:04:05"),
			UpdatedAt: podcast.UpdatedAt.Time.Format("2006-01-02 15:04:05"),
		})
	}

	return resp, nil
}
